﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using FrontRoomCheck.Utiltiy;
using YArchitech.LIB;
using YJKFamilyLibDataManager;

namespace FrontRoomCheck.ShowDataForm
{
	public partial class FrontRoomDoorReplaceForm : System.Windows.Forms.Form
	{
		public FrontRoomDoorReplaceForm(ExternalCommandData cmdData)
		{
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_cmdData = cmdData;
			this.InitializeComponent();
		}

		private void treeViewDoor_AfterCheck(object sender, TreeViewEventArgs e)
		{
			this.treeSingleCheck(this.treeViewDoor, sender, e);
		}

		private void treeViewRollingDoor_AfterCheck(object sender, TreeViewEventArgs e)
		{
			this.treeSingleCheck(this.treeViewRollingDoor, sender, e);
		}

		private void treeSingleCheck(TreeView tree, object sender, TreeViewEventArgs e)
		{
			if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)
			{
				foreach (object obj in tree.Nodes)
				{
					TreeNode treeNode = (TreeNode)obj;
					if (treeNode != e.Node)
					{
						treeNode.Checked = false;
					}
				}
			}
		}

		private void FamilyLibraryBtn_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateTreeShow();
		}

		private void CurrentProBtn_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateTreeShow();
		}

		private void UpdateTreeShow()
		{
			try
			{
				this.treeViewDoor.Nodes.Clear();
				this.treeViewRollingDoor.Nodes.Clear();
				if (!this.CurrentProBtn.Checked)
				{
					List<string> list = (from e in this.m_lstFamilyLibDoors
					select e.Name).ToList<string>();
					List<string> list2 = (from e in this.m_lstFamilyLibDoorsRolling
					select e.Name).ToList<string>();
					foreach (string text in list)
					{
						this.treeViewDoor.Nodes.Add(text);
					}
					using (List<string>.Enumerator enumerator2 = list2.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							string text2 = enumerator2.Current;
							this.treeViewRollingDoor.Nodes.Add(text2);
						}
						goto IL_265;
					}
				}
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
				List<FamilyInstance> source = filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).Cast<FamilyInstance>().Where(new Func<FamilyInstance, bool>(FrontRoomDoorReplaceForm.IsValidFireDoors)).ToList<FamilyInstance>();
				IEnumerable<string> enumerable = (from e in source
				select e.Symbol.Family.Name).Distinct<string>();
				filteredElementCollector = new FilteredElementCollector(this.m_doc);
				List<FamilyInstance> source2 = filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).Cast<FamilyInstance>().Where(new Func<FamilyInstance, bool>(FrontRoomDoorReplaceForm.IsValidFireDoorRollings)).ToList<FamilyInstance>();
				IEnumerable<string> enumerable2 = (from e in source2
				select e.Symbol.Family.Name).Distinct<string>();
				foreach (string text3 in enumerable)
				{
					this.treeViewDoor.Nodes.Add(text3);
				}
				foreach (string text4 in enumerable2)
				{
					this.treeViewRollingDoor.Nodes.Add(text4);
				}
				IL_265:;
			}
			catch
			{
			}
		}

		private static bool IsValidFireDoors(FamilyInstance fi)
		{
			string name = fi.Symbol.Family.Name;
			string name2 = fi.Symbol.Name;
			return !UtiltiyTools.IsValidDoorRollingFamilyName(name) && !UtiltiyTools.IsValidDoorRollingFamilyName(name2) && Utility.JudgeDoorIsRight(fi);
		}

		private static bool IsValidFireDoorRollings(FamilyInstance fi)
		{
			string name = fi.Symbol.Family.Name;
			string name2 = fi.Symbol.Name;
			return (UtiltiyTools.IsValidDoorRollingFamilyName(name) || UtiltiyTools.IsValidDoorRollingFamilyName(name2)) && Utility.JudgeDoorIsRight(fi);
		}

		public string FireDoorClassAFiName
		{
			get
			{
				foreach (object obj in this.treeViewDoor.Nodes)
				{
					TreeNode treeNode = (TreeNode)obj;
					if (treeNode.Checked)
					{
						return treeNode.Text;
					}
				}
				return "";
			}
		}

		public string FireDoorSuperFiName
		{
			get
			{
				foreach (object obj in this.treeViewRollingDoor.Nodes)
				{
					TreeNode treeNode = (TreeNode)obj;
					if (treeNode.Checked)
					{
						return treeNode.Text;
					}
				}
				return "";
			}
		}

		private void FrontRoomDoorReplaceForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			if (!string.IsNullOrEmpty(this.strFormCaption))
			{
				this.Text = this.strFormCaption;
			}
			this.CurrentProBtn.Checked = true;
			this.UpdateTreeShow();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void CancelBtn_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private Document m_doc;

		private ExternalCommandData m_cmdData;

		public string strFormCaption;

		public List<DbFamily> m_lstFamilyLibDoors;

		public List<DbFamily> m_lstFamilyLibDoorsRolling;
	}
}
